define([
    "./layer",
    "./http-lay",
    "conf/baseConfig",
    './tool',
    './vegetable',
    './grainOil',
    './aquatic',
    './liveStock',
    './queryObj',
    './paramsList',
    "conf/urlObj",
    "./greens",
    'polyfill'
], function(layFrame, http, baseConfig, tool, vegetable, grainOil, aquatic, liveStock, queryObj, Params, urlObj, Greens, polyfill) {
    return {
        setUserName($, target) {
            $(target).html(`${urlObj.adduser}`)
        },
        getVariety($, eltForm, eltDate, pathArr, filterBtn, container, titles, tpl, date) { //从后端获取查询条件
            const getVegtable = () => {
                const dtd = $.Deferred();
                http.interactive($).commGet(baseConfig.serUrl + pathArr[0], "", (res) => {
                    dtd.resolve(res);
                }, (err) => {
                    dtd.reject(err)
                }, urlObj.token);
                return dtd.promise();
            };
            getVegtable().then((data) => {
                http.interactive($).commGet(baseConfig.serUrl + pathArr[1], "", (res) => {
                    let obj = {
                        userType: urlObj.userType,
                        filter: filterBtn,
                        date: date,
                        varieties: {
                            title: titles[0],
                            tip: data
                        },
                        market: {
                            title: titles[1],
                            tip: tool.marketFilter(urlObj.bazaarId, res, "infoId")
                        }
                    };
                    //console.log(obj)
                    layFrame.setlayTpl("laytpl", container, tpl, obj)
                    if (date) {
                        eltForm.render()
                        eltDate.render({ //日期
                            elem: `#${date}1`,
                            range: false,
                            value: tool.pushMonth(new Date(), 1),
                            isInitValue: true,
                            trigger: 'click'
                        });

                        eltDate.render({ //日期
                            elem: `#${date}2`,
                            range: false,
                            value: tool.pushMonth(new Date(), 31),
                            isInitValue: true,
                            trigger: 'click'
                        });
                    }
                }, (err) => {
                    console.log(err)
                }, urlObj.token)
            }).then(function() {

            });
        },
        getDataList($, table, obj, url, tabConf, opt, that) {
            const self = this || that;
            const params = new Params(
                tool.testNull(obj.bazaarId),
                tool.testNull(obj.startTime),
                tool.testNull(obj.endTime),
                tool.testNull(obj.ids),
                tool.testNull(obj.pageCurrent),
                tool.testNull(obj.pageSize),
                tool.testNull(obj.state),
            );
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                // console.log(res)
                let data = [];
                $("#proId").hide();
                switch (opt) {
                    case "vegetable":
                        $.each(res.breedSelDtos, (index, item) => {
                            let veget = new Greens(
                                item.shiChangId,
                                item.pinZhongId,
                                item.jiaoCai,
                                item.waiCai,
                                item.priceLow,
                                item.priceMax,
                                item.pavg,
                                item.bazaarName,
                                item.typeName,
                                item.adduser,
                                item.addtime,
                                tool.handleStatus(item.state),
                                item.pkId);
                            data.push(veget)
                        });
                        break;
                    case "grainOil":
                        $.each(res.breedSelDtos, (index, item) => {
                            let gol = new grainOil(
                                item.shiChangId,
                                item.pinZhongId,
                                item.guiGe,
                                item.danWei,
                                item.price,
                                item.bazaarName,
                                item.typeName,
                                item.adduser,
                                item.addtime,
                                tool.handleStatus(item.state),
                                item.pkId);
                            data.push(gol)
                        });
                        break;
                    case "aquatic":
                        $.each(res.breedSelDtos, (index, item) => {
                            let aqc = new aquatic(
                                item.shiChangId - 0,
                                item.pinZhongId - 0,
                                item.guiGe,
                                item.pmax - 0,
                                item.plow - 0,
                                item.bazaarName,
                                item.typeName,
                                item.adduser,
                                item.addtime,
                                tool.handleStatus(item.state),
                                item.pkId);
                            data.push(aqc)
                        });
                        break;
                    case "liveStock":
                        $.each(res.breedSelDtos, (index, item) => {
                            let lsk = new liveStock(
                                item.areaId - 0,
                                item.pinZhongId - 0,
                                item.areaName,
                                item.typeName,
                                item.price - 0,
                                item.adduser,
                                item.addtime,
                                tool.handleStatus(item.state),
                                item.pkId);
                            data.push(lsk)
                        });
                        break;
                }
                self.renderTabeCon(table, data, tool[tabConf])

            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        getGrainOilList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = {
                bazaarId: tool.AreaIdToBazaarId(obj.bazaarId, urlObj.userType).bazaarId,
                endTime: obj.endTime ? obj.endTime : null,
                ids: obj.ids ? obj.ids : null,
                pageCurrent: obj.pageCurrent ? obj.pageCurrent : null,
                pageSize: obj.pageSize ? obj.pageSize : null,
                startTime: obj.startTime ? obj.startTime : null
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                let data = [];
                $("#proId").hide();
                $.each(res.breedSelDtos, (index, item) => {
                    const obj = {
                        pkId: item.pkId,
                        shiChangId: item.shiChangId,
                        bazaarName: item.bazaarName,
                        pinZhongId: item.pinZhongId,
                        typeName: item.typeName,
                        guiGe: item.guiGe,
                        danWei: item.danWei,
                        price: item.price,
                        adduser: item.adduser,
                        addtime: item.addtime,
                        state: tool.handleStatus(item.state)
                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        getPriceList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = {
                bazaarId: tool.AreaIdToBazaarId(obj.bazaarId, urlObj.userType).bazaarId,
                endTime: obj.endTime ? obj.endTime : null,
                ids: obj.ids ? obj.ids : null,
                pageCurrent: obj.pageCurrent ? obj.pageCurrent : null,
                pageSize: obj.pageSize ? obj.pageSize : null,
                startTime: obj.startTime ? obj.startTime : null
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                let data = [];
                // console.log(res)
                $("#proId").hide();
                $.each(res.breedSelDtos, (index, item) => {
                    const obj = {
                        pkId: item.pkId,
                        shiChangId: item.shiChangId,
                        bazaarName: item.bazaarName,
                        pinZhongId: item.pinZhongId,
                        typeName: item.typeName,
                        jiaoCai: item.jiaoCai,
                        waiCai: item.waiCai,
                        priceMax: item.priceMax,
                        priceLow: item.priceLow,
                        adduser: item.adduser,
                        addtime: item.addtime,
                        state: tool.handleStatus(item.state),
                        pavg: item.pavg
                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])

            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        getLivestockList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = {
                areaId: tool.AreaIdToBazaarId(obj.bazaarId, urlObj.userType).areaId,
                endTime: obj.endTime ? obj.endTime : null,
                ids: obj.ids ? obj.ids : null,
                pageCurrent: obj.pageCurrent ? obj.pageCurrent : null,
                pageSize: obj.pageSize ? obj.pageSize : null,
                startTime: obj.startTime ? obj.startTime : null
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                let data = [];
                $("#proId").hide();
                $.each(res.breedSelDtos, (index, item) => {
                    const obj = {
                        pkId: item.pkId,
                        areaId: item.areaId,
                        areaName: item.areaName,
                        pinZhongId: item.pinZhongId,
                        typeName: item.typeName,
                        price: item.price,
                        adduser: item.adduser,
                        addtime: item.addtime,
                        state: tool.handleStatus(item.state)
                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        getAproductList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = {
                bazaarId: tool.AreaIdToBazaarId(obj.bazaarId, urlObj.userType).bazaarId,
                endTime: obj.endTime ? obj.endTime : null,
                ids: obj.ids ? obj.ids : null,
                pageCurrent: obj.pageCurrent ? obj.pageCurrent : null,
                pageSize: obj.pageSize ? obj.pageSize : null,
                startTime: obj.startTime ? obj.startTime : null
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                let data = [];
                $("#proId").hide();
                $.each(res.breedSelDtos, (index, item) => {
                    const obj = {
                        pkId: item.pkId,
                        shiChangId: item.shiChangId,
                        bazaarName: item.bazaarName,
                        pinZhongId: item.pinZhongId,
                        typeName: item.typeName,
                        guiGe: item.guiGe,
                        priceMax: item.pmax,
                        priceLow: item.plow,
                        adduser: item.adduser,
                        addtime: item.addtime,
                        state: tool.handleStatus(item.state),
                        pavg: item.pavg
                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        getSumExcList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = {
                endTime: obj.endTime ? obj.endTime : null,
                ids: obj.ids ? obj.ids : null,
                startTime: obj.startTime ? obj.startTime : null
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                let data = [];
                $("#proId").hide();
                const checkVoluData = (data, target, sign) => {
                    let volu,
                        index = tool.serachIndexData(target, data, "object");
                    if (target === "同比") {
                        volu = data[index][sign] + "%"
                    } else {
                        volu = data[index][sign]
                    }
                    if (volu === 0) {
                        volu = `&nbsp;`
                    }
                    return volu
                };
                res.map((item) => {
                    const obj = {
                        bazaarName: item.bazaarName,
                        ciSums: checkVoluData(item.turnoverDtos, "本期", "sum"),
                        ciSuburb: checkVoluData(item.turnoverDtos, "本期", "jiaoCai"),
                        ciExternal: checkVoluData(item.turnoverDtos, "本期", "waiCai"),
                        siSums: checkVoluData(item.turnoverDtos, "同期", "sum"),
                        siSuburb: checkVoluData(item.turnoverDtos, "同期", "jiaoCai"),
                        siExternal: checkVoluData(item.turnoverDtos, "同期", "waiCai"),
                        yoySums: checkVoluData(item.turnoverDtos, "同比", "sum"),
                        yoySuburb: checkVoluData(item.turnoverDtos, "同比", "jiaoCai"),
                        yoyExternal: checkVoluData(item.turnoverDtos, "同比", "waiCai"),
                        // qsSums: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // qsSuburb: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // qsExternal: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // qsmSums: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // qsmSuburb: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // qsmExternal: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // hpSums: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // hpSuburb: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // hpExternal: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // hjxSums: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // hjxSuburb: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId),
                        // hjxExternal: checkVoluData(item.vegetableTurnoverDtos, item.vegetableId)
                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        getMultiExcList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = {
                endTime: obj.endTime ? obj.endTime : null,
                ids: obj.ids ? obj.ids : null,
                startTime: obj.startTime ? obj.startTime : null
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                console.log(res)
                let data = [];
                $("#proId").hide();
                const checkVoluData = (data, target, sign) => {
                    let volu,
                        index = tool.serachIndexData(target, data, "object");
                    if (target === "同比") {
                        volu = data[index][sign] + "%"
                    } else {
                        volu = data[index][sign]
                    }
                    if (volu === 0) {
                        volu = `&nbsp;`
                    }
                    return volu
                };
                res.map((item) => {
                    const obj = {
                        bazaarName: item.bazaarName,
                        ciSums: checkVoluData(item.turnoverDtos, "本期", "sum"),
                        ciSuburb: checkVoluData(item.turnoverDtos, "本期", "jiaoCai"),
                        ciExternal: checkVoluData(item.turnoverDtos, "本期", "waiCai"),
                        siSums: checkVoluData(item.turnoverDtos, "同期", "sum"),
                        siSuburb: checkVoluData(item.turnoverDtos, "同期", "jiaoCai"),
                        siExternal: checkVoluData(item.turnoverDtos, "同期", "waiCai"),
                        yoySums: checkVoluData(item.turnoverDtos, "同比", "sum"),
                        yoySuburb: checkVoluData(item.turnoverDtos, "同比", "jiaoCai"),
                        yoyExternal: checkVoluData(item.turnoverDtos, "同比", "waiCai"),
                        bszSums: checkVoluData(item.turnoverDtos, "白沙洲", "sum"),
                        bszSuburb: checkVoluData(item.turnoverDtos, "白沙洲", "jiaoCai"),
                        bszExternal: checkVoluData(item.turnoverDtos, "白沙洲", "waiCai"),
                        hjtSums: checkVoluData(item.turnoverDtos, "皇经堂", "sum"),
                        hjtSuburb: checkVoluData(item.turnoverDtos, "皇经堂", "jiaoCai"),
                        hjtExternal: checkVoluData(item.turnoverDtos, "皇经堂", "waiCai"),
                        sjmSums: checkVoluData(item.turnoverDtos, "四季美", "sum"),
                        sjmSuburb: checkVoluData(item.turnoverDtos, "四季美", "jiaoCai"),
                        sjmExternal: checkVoluData(item.turnoverDtos, "四季美", "waiCai")
                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        getSummaryList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = {
                endTime: obj.endTime ? obj.endTime : null,
                ids: obj.ids ? obj.ids : null,
                startTime: obj.startTime ? obj.startTime : null
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                let data = [];
                $("#proId").hide();
                const checkVoluData = (data, target, sign) => {
                    let index = tool.serachIndexData(target, data, "bazaarName");
                    let volu = data[index].turnoverDtos[0][sign];
                    if (volu === 0) {
                        volu = `&nbsp;`
                    }
                    return volu
                };
                res.map(item => {
                    const obj = {
                        bazaarName: item.vegetableName,
                        suSums: checkVoluData(item.vegetableTurnoverDtos, "合计", "sum"),
                        suSuburb: checkVoluData(item.vegetableTurnoverDtos, "合计", "jiaoCai"),
                        suExternal: checkVoluData(item.vegetableTurnoverDtos, "合计", "waiCai"),
                        bszSums: checkVoluData(item.vegetableTurnoverDtos, "白沙洲", "sum"),
                        bszSuburb: checkVoluData(item.vegetableTurnoverDtos, "白沙洲", "jiaoCai"),
                        bszExternal: checkVoluData(item.vegetableTurnoverDtos, "白沙洲", "waiCai"),
                        hjtSums: checkVoluData(item.vegetableTurnoverDtos, "皇经堂", "sum"),
                        hjtSuburb: checkVoluData(item.vegetableTurnoverDtos, "皇经堂", "jiaoCai"),
                        hjtExternal: checkVoluData(item.vegetableTurnoverDtos, "皇经堂", "waiCai"),
                        sjmSums: checkVoluData(item.vegetableTurnoverDtos, "四季美", "sum"),
                        sjmSuburb: checkVoluData(item.vegetableTurnoverDtos, "四季美", "jiaoCai"),
                        sjmExternal: checkVoluData(item.vegetableTurnoverDtos, "四季美", "waiCai"),
                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        toolbarPrice($, form, table, laydate, upload, bar, msn, initArget, urlObj, url, tabConf, arrPath, getList, opt, targetDate, tpl, upDate, tplUp) { //头工具栏事件
            const that = this;
            table.on(bar, (obj) => {
                const checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        let getCheckData = checkStatus.data,
                            status = urlObj.capacityIds.split(","),
                            ids = [];
                        if (getCheckData.length !== 0) {
                            let Examines = {}
                            layui.each(getCheckData, (index, item) => {
                                let i = tool.getStatusVal(item.state)
                                switch (i) {
                                    case 0: //未提交
                                        if (status.indexOf(`6`) !== -1) {
                                            ids.push(item.pkId)
                                            Examines = {
                                                state: 6,
                                                pkId: ids
                                            };
                                            layer.confirm("是否确认审核？", function(index) {
                                                layer.close(index);
                                                isExamineAsync()
                                            })
                                        } else {
                                            layer.msg("当前状态无法审核", { icon: 5 });
                                        }
                                        break;
                                    case 5: //中心退回
                                        if (status.indexOf(`6`) !== -1) {
                                            ids.push(item.pkId);
                                            Examines = {
                                                state: 6,
                                                pkId: ids
                                            };
                                            layer.confirm("是否确认审核？", function(index) {
                                                layer.close(index);
                                                isExamineAsync()
                                            })
                                        } else {
                                            layer.msg("当前状态无法审核", { icon: 5 });
                                        }
                                        break;
                                    case 6: //待归档
                                        if (status.indexOf(`7`) !== -1) {
                                            ids.push(item.pkId);
                                            Examines = {
                                                state: 7,
                                                pkId: ids
                                            };
                                            layer.confirm("是否确认审核？", function(index) {
                                                layer.close(index);
                                                isExamineAsync()
                                            })
                                        } else {
                                            layer.msg("当前状态无法审核", { icon: 5 });
                                        }
                                        break;
                                    case 7: //审核归档
                                        console.log(item);
                                        layer.msg("当前信息已经审核通过", { icon: 5 });
                                        break;
                                }
                            });

                            async function isExamineAsync() {
                                let obj = {
                                    $,
                                    arr: Examines,
                                    path: arrPath[0]
                                };
                                await tool.TransforPromise(that.checkInfoPrice, "isExamine", obj);
                                that[getList]($, table, urlObj, url, tabConf);
                            }
                        } else {
                            layer.msg(msn, { icon: 5 })
                        }
                        break;
                    case 'getCheckLength':
                        let getChecklist = checkStatus.data,
                            // stas = urlObj.capacityIds.split(","),
                            pids = [];
                        if (getChecklist.length !== 0) {
                            let sends = {};
                            layui.each(getChecklist, (index, item) => {
                                let i = tool.getStatusVal(item.state)
                                if (i == 6) {
                                    pids.push(item.pkId)
                                    sends = {
                                        state: 0,
                                        pkId: pids
                                    };
                                    layer.confirm("是否确认退回？", function(index) {
                                        layer.close(index);
                                        isReturnAsync()
                                    })

                                } else if (i == 7) {
                                    pids.push(item.pkId)
                                    sends = {
                                        state: 5,
                                        pkId: pids
                                    };

                                    layer.confirm("是否确认退回？", function(index) {
                                        layer.close(index);
                                        isReturnAsync()
                                    })

                                } else {
                                    console.log(item)
                                    layer.msg("当前状态无法退回", { icon: 5 });
                                }
                            });

                            async function isReturnAsync() {
                                let obj = {
                                    $,
                                    arr: sends,
                                    path: arrPath[0]
                                };
                                await tool.TransforPromise(that.checkInfoPrice, "isExamine", obj);
                                that[getList]($, table, urlObj, url, tabConf);
                            }

                        } else {
                            layer.msg(msn, { icon: 5 })
                        }
                        break;
                    case 'isRemove':
                        let removeData = checkStatus.data,
                            arr = [];
                        if (removeData.length !== 0) {
                            layui.each(removeData, (index, item) => {
                                arr.push(item.pkId)
                            });

                            async function isRemoveAsync() {
                                let obj = {
                                    $,
                                    arr,
                                    path: arrPath[1]
                                };
                                await tool.TransforPromise(that.removePrice, "isRemove", obj);
                                that[getList]($, table, urlObj, url, tabConf);
                            }

                            layer.confirm("确认删除勾选的数据？", function(index) {
                                layer.close(index);
                                isRemoveAsync()
                            })
                        } else {
                            layer.msg(msn, { icon: 5 })
                        }

                        break;
                    case 'addPrice':
                        switch (opt) {
                            case "vegetable":
                                let veget = {
                                    maketsPro: "",
                                    variety: "",
                                    jiaoCai: "",
                                    waiCai: "",
                                    plow: "",
                                    pmax: ""
                                };
                                form.val(initArget, veget);
                                break;
                            case "grainOil":
                                let gol = {
                                    maketsPro: "",
                                    variety: "",
                                    guiGe: "",
                                    danWei: "",
                                    price: ""
                                };
                                form.val(initArget, gol);
                                break;
                            case "aquatic":
                                let aqc = {
                                    maketsPro: "",
                                    variety: "",
                                    guiGe: "",
                                    priceMax: "",
                                    priceLow: ""
                                };
                                form.val(initArget, aqc);
                                break;
                            case "liveStock":
                                let lsk = {
                                    maketsPro: "",
                                    variety: "",
                                    price: ""
                                };
                                form.val(initArget, lsk);
                                break;
                        }
                        that.noticeShow($, form, '#std');
                        break;
                    case 'copyPrice':
                        let copydata = checkStatus.data;
                        if (copydata.length !== 0) {
                            let arr = []
                            switch (opt) {
                                case "vegetable":
                                    layui.each(copydata, (index, item) => {
                                        let veget = new vegetable(item.shiChangId - 0, item.pinZhongId - 0, item.jiaoCai - 0, item.waiCai - 0, item.priceLow - 0, item.priceMax - 0, urlObj.adduser, tool.isTimeNow(new Date()))
                                        arr.push(veget)
                                    });
                                    break;
                                case "grainOil":
                                    layui.each(copydata, (index, item) => {
                                        let gol = new grainOil(item.shiChangId - 0, item.pinZhongId - 0, item.guiGe, item.danWei, item.price - 0, urlObj.adduser, item.bazaarName, item.typeName, tool.isTimeNow(new Date()))
                                        arr.push(gol)
                                    });
                                    break;
                                case "aquatic":
                                    layui.each(copydata, (index, item) => {
                                        let aqc = new aquatic(item.shiChangId - 0, item.pinZhongId - 0, item.guiGe, item.priceMax - 0, item.priceLow - 0, urlObj.adduser, item.bazaarName, item.typeName, tool.isTimeNow(new Date()))
                                        arr.push(aqc)
                                    });
                                    break;
                                case "liveStock":
                                    layui.each(copydata, (index, item) => {
                                        let lsk = new liveStock(item.areaId - 0, item.pinZhongId - 0, item.areaName, item.typeName, item.price - 0, urlObj.adduser, tool.isTimeNow(new Date()))
                                        arr.push(lsk)
                                    });
                                    break;
                            }
                            async function isCopyPrice() {
                                let obj = {
                                    $,
                                    arr,
                                    path: arrPath[2]
                                }
                                await tool.TransforPromise(that.putAddInfo, "isAdd", obj)
                                that[getList]($, table, urlObj, url, tabConf, opt);
                            }

                            layer.confirm("确认复制勾选的数据？", function(index) {
                                layer.close(index);
                                isCopyPrice()
                            })

                        } else {
                            layer.msg(msn, { icon: 5 })
                        }
                        break;
                    case "setList":
                        that[getList]($, table, urlObj, url, tabConf, opt);
                        break;
                    case "cloneList":
                        that.showTimSelct(form, laydate, targetDate, tpl);
                        break;
                    case "saveData":
                        let reData = checkStatus.data,
                            list = [];
                        if (reData.length !== 0) {
                            switch (opt) {
                                case "vegetable":
                                    layui.each(reData, (index, item) => {
                                        let obj = {
                                            shiChangId: item.shiChangId,
                                            pkId: item.pkId,
                                            pinZhongId: item.pinZhongId,
                                            waiCai: item.waiCai,
                                            jiaoCai: item.jiaoCai,
                                            plow: item.priceLow,
                                            pmax: item.priceMax,
                                            pavg: item.pavg,
                                            addtime: tool.handleDate(item.addtime)
                                        };
                                        list.push(obj)
                                    });
                                    break;
                                case "grainOil":
                                    layui.each(reData, (index, item) => {
                                        let obj = {
                                            shiChangId: item.shiChangId,
                                            pkId: item.pkId,
                                            pinZhongId: item.pinZhongId,
                                            price: item.price,
                                            danWei: item.danWei,
                                            guiGe: item.guiGe,
                                            addtime: tool.handleDate(item.addtime)
                                        };
                                        list.push(obj)
                                    });
                                    break;
                                case "aquatic":
                                    layui.each(reData, (index, item) => {
                                        let obj = {
                                            shiChangId: item.shiChangId,
                                            pkId: item.pkId,
                                            pinZhongId: item.pinZhongId,
                                            guiGe: item.guiGe,
                                            plow: item.priceLow,
                                            pmax: item.priceMax,
                                            addtime: tool.handleDate(item.addtime)
                                        };
                                        list.push(obj)
                                    });
                                    break;
                                case "liveStock":
                                    layui.each(reData, (index, item) => {
                                        let obj = {
                                            areaId: item.areaId,
                                            pkId: item.pkId,
                                            pinZhongId: item.pinZhongId,
                                            price: item.price,
                                            addtime: tool.handleDate(item.addtime)
                                        };
                                        list.push(obj)
                                    });
                                    break;
                            }
                            that.SaveBatch($, arrPath[3], list)
                        } else {
                            layer.msg("请勾选您需要批量保存的数据", { icon: 5 })
                        }
                        break;
                    case 'downloadData':
                      let dwData = checkStatus.data;
                      if (dwData.length !== 0) {
                          let arr = [];
                          switch (opt) {
                              case "vegetable":
                                  layui.each(dwData, (index, item) => {
                                      let veget = new vegetable(item.shiChangId - 0, item.pinZhongId - 0, item.jiaoCai - 0, item.waiCai - 0, item.priceLow - 0, item.priceMax - 0, urlObj.adduser, tool.handleDate(item.addtime))
                                      arr.push(veget)
                                  });
                                  break;
                              case "grainOil":
                                  layui.each(dwData, (index, item) => {
                                      let gol = new grainOil(item.shiChangId - 0, item.pinZhongId - 0, item.guiGe, item.danWei, item.price - 0, urlObj.adduser, item.bazaarName, item.typeName, tool.handleDate(item.addtime))
                                      arr.push(gol)
                                  });
                                  break;
                              case "aquatic":
                                  layui.each(dwData, (index, item) => {
                                      let aqc = new aquatic(item.shiChangId - 0, item.pinZhongId - 0, item.guiGe, item.priceMax - 0, item.priceLow - 0, urlObj.adduser, item.bazaarName, item.typeName, tool.handleDate(item.addtime))
                                      arr.push(aqc)
                                  });
                                  break;
                              case "liveStock":
                                  layui.each(dwData, (index, item) => {
                                      let lsk = new liveStock(item.areaId - 0, item.pinZhongId - 0, item.areaName, item.typeName, item.price - 0, urlObj.adduser, tool.handleDate(item.addtime))
                                      arr.push(lsk)
                                  });
                                  break;
                          }
                          layer.confirm("确认下载勾选的品种？", function(index) {
                              layer.close(index);
                              // console.log(`${baseConfig.serUrl+arrPath[4]}?ids=${[...new Set(arr.map(item=>item.pinZhongId))].join(",")}`);
                              window.open(`${baseConfig.serUrl+arrPath[4]}?ids=${[...new Set(arr.map(item=>item.pinZhongId))].join(",")}`);
                          })
                      } else {
                          // console.log(`${baseConfig.serUrl+arrPath[4]}`);
                          window.open(`${baseConfig.serUrl+arrPath[4]}`);
                      }
                      break;
                    case "upList":
                        that.upTimSelct($, table, form, laydate, upload, upDate, tplUp, url, arrPath[5], tabConf, opt);
                        break;
                    case "addUser":
                      console.log('addUser')
                      break;
                    case "editUser":
                      console.log('editUser')
                      break;
                    case "removeUser":
                      console.log('removeUser')
                      break;
                }
            });
        },
        getStatistisData($, table, obj, url, tabConf, that) { //获取统计汇总
            const self = this || that;
            const params = {
                bazaarId: obj.bazaarId,
                endTime: obj.endTime,
                ids: obj.ids,
                startTime: obj.startTime
            };
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                let data = [];
                $("#proId").hide();
                $.each(res, (index, item) => {
                    const obj = {
                        name: item.name,
                        currentPeriod: item.currentPeriod,
                        priorPeriod: item.priorPeriod,
                        correspondPeriod: item.correspondPeriod,
                        chainIndex: item.chainIndex,
                        correspondIndex: item.correspondIndex
                    };
                    data.push(obj)
                });
                self.renderTabeCensus(table, data, tool[tabConf])
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        submitTip($, table, form, target, url, hanldFn, tabConf, opt) { //监听提交
            const that = this;

            form.verify({
                staDate(value) {
                    console.log(value)
                }
            });
            form.on(`submit(${target})`, (data) => {
                let arr = [],
                    bazzs = [];
                if (data.field.staDate || data.field.endDate) {
                    let sta = tool.transTime(data.field.staDate);
                    let end = tool.transTime(data.field.endDate);
                    if (end - sta < 0) {
                        layFrame.showMsg("时间先后有问题，请重填", 0, 3000)
                        return
                    }
                } else {
                    layFrame.showMsg("时间不能为空", 0, 3000)
                    return
                }
                if (data.field.interest === "") {
                    // layFrame.showMsg("请选择市场", 0, 3000)
                    // return
                    data.field.interest = null
                }
                $.each(data.field, (key, item) => {
                    if (item === "on") {
                        if (key !== "close") {
                            arr.push(key)
                        }
                    }
                });
                if (data.field.interest) {
                    bazzs.push(data.field.interest);
                } else {
                    bazzs = data.field.interest
                }
                if (!arr.length) {
                    layFrame.showMsg("请选择您要查询的品种", 0, 3000);
                    return
                } else {
                    switch (opt) {
                        case "vegetable":
                            let veget = new queryObj(bazzs, data.field.endDate, arr, urlObj.pageCurrent, urlObj.pageSize, data.field.staDate, data.field.interest)
                            hanldFn($, table, veget, url, tabConf, that)
                            break;
                        case "grainOil":
                            let gol = new queryObj(bazzs, data.field.endDate, arr, urlObj.pageCurrent, urlObj.pageSize, data.field.staDate, data.field.interest)
                            hanldFn($, table, gol, url, tabConf, that)
                            break;
                        case "aquatic":
                            let aqc = new queryObj(bazzs, data.field.endDate, arr, urlObj.pageCurrent, urlObj.pageSize, data.field.staDate, data.field.interest)
                            hanldFn($, table, aqc, url, tabConf, that)
                            break;
                        case "liveStock":
                            let lsk = new queryObj(bazzs, data.field.endDate, arr, urlObj.pageCurrent, urlObj.pageSize, data.field.staDate, data.field.interest)
                            hanldFn($, table, lsk, url, tabConf, that)
                            break;
                    }
                }
                return false;
            });
        },
        renderTabeCon(table, data, tabConf) {
            table.render(tabConf(data));
        },
        renderTabeCensus(table, data, tabSta) {
            table.render(tabSta(data));
        },
        noticeShow($, form, target) { //触发弹框事件
            this.indexOpn = layer.open({
                type: 1,
                title: "编辑信息",
                content: $(target),
                success: function(layero) {
                    form.render()
                },
                yes: function(index, layero) {
                    layer.close(index);
                }
            });
        },
        handeTool($, form, table, target, mge, initArget, url, tabConf, delPath, getList, opt) { //监听行工具事件
            const that = this;
            table.on(target, (obj) => {
                const data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm(mge, function(index) {
                        // that.removePrice($, data.pkId, delPath)
                        obj.del(data);
                        layer.close(index);
                        async function isRemoveAsync() {
                            let obj = {
                                $,
                                arr: data.pkId,
                                path: delPath
                            };
                            await tool.TransforPromise(that.removePrice, "isRemove", obj);
                            that[getList]($, table, urlObj, url, tabConf);
                        }

                        isRemoveAsync()

                    });
                } else if (obj.event === 'edit') {
                    switch (opt) {
                        case "vegetable":
                            //let veget = new vegetable(data.shiChangId, data.pinZhongId, data.jiaoCai, data.waiCai, data.priceLow, data.priceLow, urlObj.adduser)
                            let veget = {
                                maketsPro: data.shiChangId,
                                variety: data.pinZhongId,
                                plow: data.priceLow,
                                pmax: data.priceMax,
                                jiaoCai: data.jiaoCai,
                                waiCai: data.waiCai,
                                pkId: data.pkId
                            }
                            form.val(initArget, veget)
                            break;
                        case "grainOil":
                            //let gol = new grainOil(data.shiChangId, data.pinZhongId, data.guiGe, data.danWei, data.price, urlObj.adduser)
                            let gol = {
                                maketsPro: data.shiChangId,
                                variety: data.pinZhongId,
                                guiGe: data.guiGe,
                                danWei: data.danWei,
                                price: data.price,
                                pkId: data.pkId
                            }
                            form.val(initArget, gol)
                            break;
                        case "aquatic":
                            //let aqc = new aquatic(data.shiChangId, data.pinZhongId, data.guiGe, data.priceMax, data.priceLow, urlObj.adduser)
                            let aqc = {
                                maketsPro: data.shiChangId,
                                variety: data.pinZhongId,
                                guiGe: data.guiGe,
                                priceMax: data.priceMax,
                                priceLow: data.priceLow,
                                pkId: data.pkId
                            }
                            form.val(initArget, aqc)
                            break;
                        case "liveStock":
                            //let lsk = new liveStock(data.shiChangId, data.pinZhongId, data.price, urlObj.adduser)
                            let lsk = {
                                maketsPro: data.areaId,
                                variety: data.pinZhongId,
                                price: data.price,
                                pkId: data.pkId
                            }
                            form.val(initArget, lsk)
                            break;
                    }
                    this.noticeShow($, form, '#etd')

                }
            });
        },
        checkInfoPrice($, obj, url, resolve) { //审核提交,批量退回
            let params
            if (tool.isArrayFn(obj.pkId)) {
                params = obj.pkId
            } else {
                params = []
                params.push(obj.pkId)
            }
            http.interactive($).commPost(baseConfig.serUrl + `${url}?state=${obj.state}`, params, (res) => {
                console.log(res);
                resolve()
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        removePrice($, obj, url, resolve) { //删除信息操作
            let params
            if (tool.isArrayFn(obj)) {
                params = obj
            } else {
                params = []
                params.push(obj)
            }
            console.log(params)
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                console.log(res)
                resolve()
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        putAddInfo($, obj, url, resolve) { //新增一条
            let params
            if (tool.isArrayFn(obj)) {
                params = obj
            } else {
                params = []
                params.push(obj)
            }
            //console.log(params)
            http.interactive($).commPut(baseConfig.serUrl + url, params, (res) => {
                console.log(res)
                resolve()
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        putEditInfo($, obj, url) { //编辑当前一条信息
            let params = obj
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                console.log(params)
                console.log(res)
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        submitAddPrice($, table, form, target, hanldFn, url, tabConf, path, listFn, second, opt) { //表单btn提交新增信息
            const that = this;
            const date = new Date();
            let hh = date.getHours();
            let mm = date.getMinutes();
            let ss = date.getSeconds();
            let hms = `${tool.TransDateout(hh)}:${tool.TransDateout(mm)}:${tool.TransDateout(ss)}`;
            tool.verifyInput(form, opt); //对（1.蔬菜 2.粮油 3.水产 4畜禽  新增值进行验证）
            form.on(`submit(${target})`, (data) => {
                let comfi = confirm("请检输入项对应的单位，是否提交"),
                    price;
                if (comfi) {
                    switch (opt) {
                        case "vegetable":
                            price = new vegetable(
                                data.field.maketsPro - 0,
                                data.field.variety - 0,
                                data.field.jiaoCai - 0,
                                data.field.waiCai - 0,
                                data.field.pmax - 0,
                                data.field.plow - 0,
                                urlObj.adduser,
                                `${data.field.addtime} ${hms}`,
                                data.field.pkId - 0);
                            break;
                        case "grainOil":
                            price = new grainOil(
                                data.field.maketsPro - 0,
                                data.field.variety - 0,
                                data.field.guiGe,
                                data.field.danWei,
                                data.field.price - 0,
                                urlObj.adduser, "", "",
                                `${data.field.addtime} ${hms}`, "",
                                data.field.pkId - 0);
                            break;
                        case "aquatic":
                            price = new aquatic(
                                data.field.maketsPro - 0,
                                data.field.variety - 0,
                                data.field.guiGe,
                                data.field.priceMax - 0,
                                data.field.priceLow - 0,
                                urlObj.adduser, "", "",
                                `${data.field.addtime} ${hms}`, "",
                                data.field.pkId - 0);
                            break;
                        case "liveStock":
                            price = new liveStock(
                                data.field.maketsPro - 0,
                                data.field.variety - 0, "", "",
                                data.field.price - 0,
                                urlObj.adduser,
                                `${data.field.addtime} ${hms}`, "",
                                data.field.pkId - 0);
                            break;
                    }
                    async function subAddasync() {
                        let obj = {
                            $,
                            arr: price,
                            path: url
                        };
                        await tool.TransforPromise(hanldFn, "isAdd", obj)
                        that[listFn]($, table, urlObj, path, tabConf, that)
                    }
                    subAddasync()
                }
                if (target === "demo4" || "demo5") {
                    layer.close(this.indexOpn);
                }
                return false;
            });
        },
        checkSwitch($, form, laydate, checkFun, swhFun, btn, target, type, attr, tpl, getVariety, searchId, pathArr, container, titles, date) {
            const that = this
                //有一个未选中 取消全选
            form.on(checkFun, function(data) {
                let item = $(target);
                for (let i = 0; i < item.length; i++) {
                    if (item[i].checked === false) {
                        $(btn).prop(attr, false);
                        form.render(type);
                        break;
                    }
                }
                //如果都勾选了  勾上全选
                let all = item.length;
                for (let i = 0; i < all; i++) {
                    if (item[i].checked === true) {
                        all--;
                    }
                }
                if (all === 0) {
                    $(btn).prop(attr, true);
                    form.render(type);
                }
            });
            switch (swhFun) {
                case "switch(switchTest)":
                    //反选
                    form.on(swhFun, function(data) {
                        if (this.checked) {
                            let a = data.elem.checked;
                            if (a === true) {
                                $(target).prop(attr, true);
                                form.render(type);
                            } else {
                                $(target).prop(attr, false);
                                form.render(type);
                            }
                        } else {
                            let item = $(target);
                            item.each(function() {
                                if ($(this).prop(attr)) {
                                    $(this).prop(attr, false);
                                } else {
                                    $(this).prop(attr, true);
                                }
                            });
                            form.render(type);
                        }
                    });
                    break;
                case "switch(switchUesd)":
                    //反选
                    form.on(swhFun, function(data) {
                        if (this.checked) {
                            let a = data.elem.checked;
                            if (a === true) {
                                //$(target).prop(attr, true);
                                that[getVariety]($, form, laydate, pathArr, searchId, container, titles, tpl, date)
                                form.render(type);
                            } else {
                                $(target).prop(attr, false);
                                form.render(type);
                            }
                        } else {
                            let item = $(target);
                            item.each(function() {
                                if ($(this).prop(attr)) {
                                    $(this).prop(attr, false);
                                } else {
                                    $(this).prop(attr, true);
                                }
                            });
                            form.render(type);
                        }
                    });
                    break;
            }
        },
        clonePrice($, url, cloneDate, addDate) {
            let district;
            switch (urlObj.userType) {
                case "A":
                    district = tool.AreaIdToBazaarId(urlObj.bazaarId, urlObj.userType).areaId;
                    break;
                case "B":
                    district = tool.AreaIdToBazaarId(urlObj.bazaarId, urlObj.userType).bazaarId;
                    break;
                default:
                    district = [];
                    break;
            }
            let params = {
                cloneDate: cloneDate,
                district: district[0],
                userName: urlObj.adduser,
                addtime: addDate
            }
            http.interactive($).commPut(baseConfig.serUrl + url, params, (res) => {
                //console.log(res)
                layFrame.showMsg(res.msg, 1, 3000)
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        SaveBatch($, url, data) {
            let params = data
            http.interactive($).commPost(baseConfig.serUrl + url, params, (res) => {
                console.log(res)
                layFrame.showMsg(res.msg, 1, 3000)
            }, (err) => {
                console.log(err)
            }, urlObj.token)
        },
        listenTab($, table, fliter) { //监听单元格编辑
            table.on(fliter, function(obj) {
                // console.log(obj)
                let value = obj.value //得到修改后的值
                    ,
                    data = obj.data //得到所在行所有键值
                    ,
                    field = obj.field; //得到字段
                layer.msg('[品种名称]: ' + data.typeName + '] ' + field + ' 字段更改为：' + value);
            });
        },
        setDefdate($, form, laydate, target) {
            form.render();
            laydate.render({ //日期
                elem: `#${target}`,
                range: false,
                value: new Date(),
                isInitValue: true
            });
        },
        SubmitBatch($, table, form, target, url, tabConf, state, opt) { //提交查询时间（批量修改）
            const that = this;
            form.on(`submit(${target})`, (data) => {
                let obj = {
                    bazaarId: urlObj.bazaarId,
                    startTime: data.field.staDate,
                    endTime: data.field.staDate,
                    ids: urlObj.ids,
                    pageCurrent: urlObj.pageCurrent,
                    pageSize: urlObj.pageSize,
                    state: state
                };
                that.getDataList($, table, obj, url, tabConf, opt, that)
            })
        },
        showTimSelct(form, laydate, target, tpl) {
            this.indexCln = layer.open({
                type: 1,
                title: "克隆数据",
                content: tpl,
                id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    ,
                btnAlign: 'c',
                success: function(layer) {
                    form.render();
                    laydate.render({ //指定日期
                        elem: `#${target}`,
                        range: false,
                        value: new Date(),
                        isInitValue: true,
                        trigger: 'click'
                    });

                    laydate.render({ //上报日期
                        elem: `#${target}1`,
                        range: false,
                        value: new Date(),
                        isInitValue: true,
                        trigger: 'click'
                    });
                },
                yes: function(index, layero) {
                    layer.close(index);

                }
            });
        },
        upTimSelct($, table, form, laydate, upload, target, tpl, url, upUrl, tabConf, opt) {
            const that = this;
            let time;
            that.indexUp = layer.open({
                type: 1,
                title: "上传数据",
                content: tpl,
                id: 'LAY_layuipro_up' //设定一个id，防止重复弹出
                    ,
                btnAlign: 'c',
                success: function(layero) {
                    form.render();
                    let p = new Promise((resolve, reject) => {
                        laydate.render({ //指定日期
                            elem: `#${target}`,
                            range: false,
                            value: new Date(),
                            isInitValue: true,
                            trigger: 'click',
                            done: function(value, date, endDate) {
                                resolve(value);
                                document.querySelector("#dataUp").disabled = "disabled";
                                document.querySelector("#dataUp").style.cursor = "not-allowed"
                            }
                        });
                    });

                    p.then(time => {
                        // console.log(tool.addtoSec(time));
                        let pt = new Promise((resolve, reject) => {
                            upload.render({ //选完文件后不自动上传
                                elem: '#test8',
                                url: `${baseConfig.serUrl}${upUrl}?bazaarId=${urlObj.bazaarId[0]}&userName=${urlObj.adduser}&time=${time?tool.addtoSec(time):tool.isTimeNow(new Date())}`,
                                method: 'post',
                                auto: false
                                    //,multiple: true
                                    ,
                                accept: 'file'
                                    // ,data:{}
                                    ,
                                headers: { token: urlObj.token },
                                bindAction: '#test9',
                                done: function(res) {
                                    switch (res.code) {
                                        case 0:
                                            layer.msg(`${res.msg}, 请关闭对话框`, { icon: 1 });
                                            layer.close(that.indexUp);
                                            resolve(res.code)
                                            break;
                                        case 400:
                                            layer.msg(`${res.msg}, 请检查excel模板数据，再次上传`, { icon: 5 });
                                            break;
                                        case 405:
                                            layer.msg(`${res.msg}， 稍后上传`, { icon: 5 });
                                            break;
                                    }
                                }
                            });
                        })
                        pt.then(data => {
                            that.getDataList($, table, urlObj, url, tabConf, opt, that)
                        })
                    }).catch(error => console.log(error));
                },
                yes: function(index) {
                    layer.close(index);

                }
            });
        },
        SubmitClone($, form, table, target, url, listFn, path, tabConf, second, opt) { //提交查询时间（批量修改）
            const that = this;
            form.on(`submit(${target})`, (data) => {
                that.clonePrice($, url, data.field.staDate, data.field.addDate);
                layer.close(this.indexCln);
                setTimeout(function() {
                    that[listFn]($, table, urlObj, path, tabConf, opt, that)
                }, second);
                return false

            })
        },
        getUserList($, table, obj, url, tabConf, that) {
            const self = this || that;
            const params = obj.ids ? obj.ids : [];;
            http.interactive($).commPost(baseConfig.serUrl + url, [], (res) => {
                let data = [];
                // console.log(res)
                $("#proId").hide();
                $.each(res, (index, item) => {
                    const obj = {
                        id: item.id,
                        userName: item.userName,
                        byName: item.byName,
                        type: item.type,
                        roleName: item.roleName,
                        bazaarIds: item.bazaarIds,
                        capacityIds: item.capacityIds,

                    };
                    data.push(obj)
                });
                self.renderTabeCon(table, data, tool[tabConf])

            }, (err) => {
                console.log(err)
            }, urlObj.token)
        }
    }
})